const formatTime = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()
	return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : '0' + n
}
//时间戳转换成时间
function _date_time(unixtime, code = false) {
	var date = new Date(unixtime);
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	var d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	var h = date.getHours();
	h = h < 10 ? ('0' + h) : h;
	var minute = date.getMinutes();
	var second = date.getSeconds();
	minute = minute < 10 ? ('0' + minute) : minute;
	second = second < 10 ? ('0' + second) : second;
	// console.log(h + ':' + minute + ':' + second)
	if (code) return y + '-' + m + '-' + d + ' ' + h + ':' + minute; //年月日时分秒
	return y + '-' + m + '-' + d
}


// 删除数组中某个元素
const arrRemoveObj = (array, obj) => {
	let length = array.length;
	for (let i = 0; i < length; i++) {
		if (array[i] === obj) {
			if (i === 0) {
				array.shift();
				return array;
			} else if (i === length - 1) {
				array.pop();
				return array;
			} else {
				array.splice(i, 1);
				return array;
			}
		}
	}
}

function timeFormat (date){
	let y=0
	let m=0
	let d=0
	if(date>3600){
		y=parseInt(date/3600)
		date=date%3600
	}
	if(date>60){
		m=parseInt(date/60)
	}		
		d=date%60
		return`${y?y+'时':''}${m?m+'′':''}${d||'00'}″`
	 
}


// date工具
const dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function(milliseconds) {
		// console.log(milliseconds)
		milliseconds = new Date().getTime() - milliseconds
		let humanize = '';
		for (let key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		let a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	},
	format: function(date) {
		date = new Date(date);
		let diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		let _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '-' + _format(date.getMonth() + 1) + '-' + _format(date.getDate()) + ' ' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
}

function timeText(date2,date3) {
		let date=new Date(date2 + ' ' + date3).getTime()
	let time = new Date().getTime()
	let day = 86400000
	let day_ = parseInt((date - time) / day)
	let text=date2 + ' ' + date3

		if(date2==formatDate(time)){
			text='今天'
		}else if(date2==formatDate(time+day)){
			text='明天'
		}else if(date2==formatDate(time+day+day)){
			text='后天'
		}else{
			 return text
		}
		if(date3.split(':')[0]>12){
			text+='下午 '
		}else{
			text+='上午 '
		}
		text+=date3
   return text
}
//时间戳转日期
function formatDate(date) {
  var date = new Date(date);
  var YY = date.getFullYear() + '-';
  var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  return YY + MM + DD
}
//预览图片
function enlargement(data, index) {
	uni.previewImage({
		urls: data,
		current: index,
		longPressActions: {
			itemList: ['发送给朋友', '保存图片'],
			success: function(data) {
			
			},
		}
	});
}
	const innerAudioContext = uni.createInnerAudioContext();
function 	knock(){
	// innerAudioContext.stop()
	// 	innerAudioContext.destroy();
	// 	innerAudioContext.src = 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-3440546a-c254-4f15-9a99-d12987be7570/d246bd50-acdf-429d-8dba-05718a483232.mp3'
	// 	innerAudioContext.play()
	// 	innerAudioContext.onPlay(() => {
	// 		console.log('开始播放');
	// 	});
		
}


module.exports = {
	_date_time,
	formatTime: formatTime,
	dateUtils: dateUtils,
	arrRemoveObj: arrRemoveObj,
	enlargement,
	timeText,
	
	knock,
	timeFormat
}
